package com.legend.ddz.util;

import com.legend.ddz.DDZ;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;

/**
 * 
 * @author zhugh
 * @date 2012-6-27 下午3:15:21
 * @description TODO
 * 
 */

public class HomeMenuView extends SurfaceView implements SurfaceHolder.Callback, OnTouchListener, Runnable
{

	private DDZ ddz;
	SurfaceHolder holder;
	Canvas canvas;
	Bitmap bitmap;
	Thread thread;
	volatile boolean threadFlag = true; 

	public HomeMenuView(Context context, DDZ ddz)
	{
		super(context);
		this.ddz = ddz;  
		holder = getHolder();
		this.getHolder().addCallback(this);
		this.setOnTouchListener(this);
		
	}

	@Override
	public boolean onTouch(View v, MotionEvent event)
	{
		return false;
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder)
	{
		this.threadFlag = true;
		Thread localThread = new Thread(this);
		this.thread = localThread;
		this.thread.start();
		bitmap = Bitmap.createBitmap(800, 480,Config.ARGB_8888); 
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
	{

	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder)
	{
		this.threadFlag = false;
		boolean retry = true;
		while (retry)
		{
			try
			{
				thread.join();//等待线程结束
			} catch (InterruptedException e)
			{
				e.printStackTrace();
			}
			retry = false;
		}//循环刷新，直到线程结束为止

	}
	


	@Override
	public void run()
	{
		if (threadFlag)
		{
			try
			{
				canvas = holder.lockCanvas();
				synchronized (this)
				{
					onDraw(canvas);
				}
			} finally
			{
				holder.unlockCanvasAndPost(canvas);
			}
			try
			{
				Thread.sleep(100);
			} catch (InterruptedException e)
			{
				e.printStackTrace();
			}
		}

	}
	
	@Override
	protected void onDraw(Canvas canvas)
	{
		super.onDraw(canvas);
		Paint paint = new Paint();  
	}
	

}
